/**
 * 
 */
package ikeuilib;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;



/**
 * @author ikechan
 *
 */
public class JFontDialog extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JList[] list;
	private JLabel label;
	private boolean isCanceld=true;
	private JScrollBar[] scroll;
	
	public JFontDialog(){
		super(JOptionPane.getRootFrame(),true);
		super.setTitle("Font Dialog");
		super.getContentPane().setLayout(new GridLayout(2,1));
		JPanel up=new JPanel(new GridLayout(1,3));
		JPanel bt=new JPanel(new GridLayout(2,1));
		list=new JList[3];
		JScrollPane[] sp=new JScrollPane[3];
		Font font=new Font("Dialog",Font.PLAIN,10);
		scroll=new JScrollBar[3];
		for(int i=0;i<list.length;i++){
			list[i]=new JList();
			list[i].setFont(font);
			sp[i]=new JScrollPane(list[i]);
			sp[i].setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			scroll[i]=sp[i].getVerticalScrollBar();
		}
		sp[0].setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(Color.GRAY), "Family"));
		sp[1].setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(Color.GRAY), "Style"));
		sp[2].setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(Color.GRAY), "Size"));
		for(int i=0;i<sp.length;i++)up.add(sp[i]);
		super.getContentPane().add(up);
		label=new JLabel("ABCabc123あいう");
		bt.add(label);
		JPanel bp=new JPanel();
		BoxLayout bl=new BoxLayout(bp,BoxLayout.X_AXIS);
		bp.setLayout(bl);
		bp.add(Box.createGlue());
		JButton bt0=new JButton(" O  K ");
		bt0.setFont(font);
		JButton bt1=new JButton("Cancel");
		bt0.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				isCanceld=false;
				setVisible(false);
			}
		});
		bt1.setFont(font);
		bt1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				isCanceld=true;
				setVisible(false);
			}
		});
		bp.add(bt0);
		bp.add(bt1);
		bt.add(bp);
		super.getContentPane().add(bt);
		String[] sysFont=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
		String[] family=new String[sysFont.length+5];
		family[0]="Dialog";
		family[1]="DialogInput";
		family[2]="Monospaced";
		family[3]="Serif";
		family[4]="SansSerif";
		for(int i=0;i<sysFont.length;i++)family[i+5]=sysFont[i];
		String[] style={"PLAIN","BOLD","ITALIC","ITALIC-BOLD"};
        String[] size={"4","5","6","7","8","9","10","11","12","13","14","15",
                "16","18","20","22","24","28","32","36","40","44","48"};
        list[0].setListData(family);
        list[1].setListData(style);
        list[2].setListData(size);
        for(int i=0;i<list.length;i++)list[i].setSelectedIndex(0);
        ListSelectionListener ll=new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent arg0) {
				Font f=getFontToList();
				label.setFont(f);
			}
        };
       for(int i=0;i<list.length;i++)list[i].addListSelectionListener(ll);
       super.setSize(350, 250);
	}
	
	private Font getFontToList(){
		String family=(String)list[0].getSelectedValue();
		int style=Font.PLAIN;
		switch(list[1].getSelectedIndex()){
			case 0:
				style=Font.PLAIN;
				break;
			case 1:
				style=Font.BOLD;
				break;
			case 2:
				style=Font.ITALIC;
				break;
			default:
				style=Font.ITALIC+Font.BOLD;
			break;
		}
		int size=Integer.parseInt((String)list[2].getSelectedValue());
		Font ret=new Font(family,style,size);
		return ret;
	}
	
	public Font getFont(Component c){
		super.setLocationRelativeTo(c);
		Font f=c.getFont();
		if(f!=null){
			String family=f.getFamily();
			list[0].setSelectedValue(family, true);
			setScrollBar(0,list[0].getSelectedIndex());
			int style=f.getStyle();
			switch(style){
				case Font.PLAIN:
					list[1].setSelectedIndex(0);
					break;
				case Font.BOLD:
					list[1].setSelectedIndex(1);
					break;
				case Font.ITALIC:
					list[1].setSelectedIndex(2);
					break;
				default:
					list[1].setSelectedIndex(3);
					break;
			}
			setScrollBar(1,list[1].getSelectedIndex());
			int size=f.getSize();
			list[2].setSelectedValue(Integer.toString(size), true);
			setScrollBar(2,list[2].getSelectedIndex());
		}else{
			list[0].setSelectedIndex(0);
			list[1].setSelectedIndex(0);
			list[2].setSelectedIndex(6);
			setScrollBar(2,list[2].getSelectedIndex());
		}
		super.setVisible(true);
		if(isCanceld){
			return null;
		}else{
			return getFontToList();
		}
	}
	
	private void setScrollBar(int id,int index){
		FontMetrics fm=getFontMetrics(list[id].getFont());
		int h=fm.getHeight();
		scroll[id].setValue(index*h);
	}
	
	public static Font chooseFont(Component c){
		JFontDialog f=new JFontDialog();
		return f.getFont(c);
	}

	public static void main(String[] args){
		Font font=JFontDialog.chooseFont(JOptionPane.getRootFrame());
		System.out.println(font);
		System.exit(0);
	}
}
